﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace huqiang.Data
{
    public class ManifestReader
    {
        struct KeyValues
        {
            public string key;
            public string[] values;
        }
        List<KeyValues> keyValues;
        public void LoadFromData(byte[] dat)
        {
            string str = Encoding.UTF8.GetString(dat);
            Load(str);
        }
        public void LoadFromFile(string path)
        {
            if (File.Exists(path))
            {
                string str = File.ReadAllText(path);
                Load(str);
            }
        }
        void Load(string str)
        {
            string[] ss = str.Split('\n');
            keyValues = new List<KeyValues>();
            for (int i = 0; i < ss.Length; i++)
            {
                string[] a = ss[i].Split(':');
                KeyValues kv = new KeyValues();
                kv.key = a[0];
                if (a.Length > 1)
                    kv.values = a[1].Split(',');
                keyValues.Add(kv);
            }
        }
        public void GetDependence(List<string> list, string key)
        {
            for (int i = 0; i < keyValues.Count; i++)
                if (keyValues[i].key == key)
                {
                    var v = keyValues[i].values;
                    if (v != null)
                        for (int j = 0; j < v.Length; j++)
                        {
                            string o = v[j];
                            if (o != null & o != "")
                                if (!list.Contains(o))
                                {
                                    list.Add(o);
                                    GetDependence(list, o);
                                }
                        }
                    return;
                }
        }
        public void Add(string key, string[] values)
        {
            if (keyValues == null)
                keyValues = new List<KeyValues>();
            KeyValues kv = new KeyValues();
            kv.key = key;
            kv.values = values;
            for (int i = 0; i < keyValues.Count; i++)
            {
                if (keyValues[i].key == key)
                {
                    keyValues[i] = kv;
                    return;
                }
            }
            keyValues.Add(kv);
        }
        public void WriteToFile(string path)
        {
            if (keyValues == null)
                return;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < keyValues.Count; i++)
            {
                if (i > 0)
                    sb.Append('\n');
                var kv = keyValues[i];
                sb.Append(kv.key);
                sb.Append(':');
                if (kv.values != null)
                {
                    int s = 0;
                    for (int j = 0; j < kv.values.Length; j++)
                    {
                        var v = kv.values[j];
                        if (v != null)
                        {
                            if (s > 0)
                                sb.Append(',');
                            sb.Append(v);
                            s++;
                        }
                    }
                }
            }
            File.WriteAllText(path, sb.ToString());
        }
        public void Clear(List<string> now)
        {
            if (keyValues == null)
                return;
            int c = keyValues.Count - 1;
            for (; c >= 0; c--)
            {
                if (!now.Contains(keyValues[c].key))
                    keyValues.RemoveAt(c);
            }
        }
        public void GetKeys(List<string> keys)
        {
            if (keyValues == null)
                return;
            for (int i = 0; i < keyValues.Count; i++)
            {
                if (!keys.Contains(keyValues[i].key))
                {
                    keys.Add(keyValues[i].key);
                }
                var v = keyValues[i].values;
                if (v != null)
                {
                    for (int j = 0; j < v.Length; j++)
                    {
                        if (!keys.Contains(v[j]))
                        {
                            keys.Add(v[j]);
                        }
                    }
                }
            }
        }
    }
}

